package com.parenting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.entities.OrderApprovalDO;
import com.parenting.enums.ResultEnum;
import com.parenting.exception.BaseException;
import com.parenting.mapper.OrderApprovalMapper;
import com.parenting.model.IMPage;
import com.parenting.request.platform.SearchOrderApproval;
import com.parenting.response.OrderApprovalResponse;
import com.parenting.response.platform.ApprovalResponse;
import com.parenting.service.OrderApprovalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parenting.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 平台审核退款 服务实现类
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-10-25
 */
@Service
@Slf4j
public class OrderApprovalServiceImpl extends ServiceImpl<OrderApprovalMapper, OrderApprovalDO> implements OrderApprovalService {

    @Override
    public IMPage<OrderApprovalResponse> getOrderApprovalList(SearchOrderApproval searchOrderApproval) {
        LambdaQueryWrapper<OrderApprovalDO> wrapper=new LambdaQueryWrapper();
          if (null != searchOrderApproval.getOrderState()){
              wrapper.eq(OrderApprovalDO::getOrderState,searchOrderApproval.getOrderState());
          }
        IPage<OrderApprovalDO> orderApprovalDOIPage = baseMapper.selectPage(searchOrderApproval.getPage(), wrapper);
        IMPage<OrderApprovalResponse> imPage=new IMPage<>();
        List<OrderApprovalResponse> list=new ArrayList<>();
          if (orderApprovalDOIPage.getTotal()>0){
              for (OrderApprovalDO o:orderApprovalDOIPage.getRecords()) {
                  list.add(BeanUtil.copyProperties(o,OrderApprovalResponse.class));
              }
              imPage.setRecords(list);
          }
          imPage.setTotal(orderApprovalDOIPage.getTotal());
          imPage.setCurrent(orderApprovalDOIPage.getCurrent());
          imPage.setSize(orderApprovalDOIPage.getSize());
        return imPage;
    }

    @Override
    public ApprovalResponse getOrderApproval(String orderApprovalId) {
        return baseMapper.getOrderApproval(orderApprovalId);
    }
    @Autowired
    private OrdersService ordersService;

    @Transactional
    @Override
    public void agree(String orderApprovalId) {
        OrderApprovalDO orderApprovalDO = baseMapper.selectById(orderApprovalId);
        try {
            ordersService.ByRefund(orderApprovalDO.getOrderId());
        }catch (Exception e){
            log.info("===================================== { } ", "订单退款异常！");
            throw  new BaseException(ResultEnum.ORDER_REFUSE_REFUND);
        }
        orderApprovalDO.setUpdatedTime(new Date());
        orderApprovalDO.setOrderState(2);
        baseMapper.updateById(orderApprovalDO);
    }
}
